//
//  GenerateQRCodeViewController.m
//  BarCodeAndQRCode
//
//  Created by jiezsoft.com on 14-10-10.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.

#import "GenerateQRCodeViewController.h"
#import "QRCodeGenerator.h"
#import "AddMemberViewController.h"
#import "MembershipDetailsViewController.h"
#import "MemberListViewController.h"
#import "OrderExtAddViewController.h"
#import "ReabamSingleton.h"
@interface GenerateQRCodeViewController ()<UIScrollViewDelegate,UIAlertViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate>

@end

@implementation GenerateQRCodeViewController
{
    UIScrollView*mainScroll;
    ReabamSingleton*commdity;
    UIButton *time;
    NSString *sn;
 
}
@synthesize mDelegate;
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    commdity=[[ReabamSingleton shared]init];
//
//}
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
    [self setNavigation];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
//  NSString* companyID = [user objectForKey:@"companyId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
//    if ([[Global getType] isEqualToString:@"GetCompanyQrCode"]) {
//          [params setValue:companyID forKey:@"companyID"];
//    }
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
//     NSLog(@"-----%@",[Global getType]);
    
    [HttpRequest requestData:[NSString stringWithFormat:@"Company/%@" , @"GetReadMemberQrCode"]  params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                NSLog(@"%@",severDataDict.dataDict);
               
                sn=[severDataDict.dataDict  objectForKey:@"sn"];
                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                [user setObject:severDataDict.dataDict forKey:@"qr"];
                [user synchronize];
                
                UIButton* QRCode=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth*85/750,ScreenHeight*370/1334, 385/2,385/2) ];
                QRCode.center=self.view.center;
                [self.view addSubview:QRCode];
                [self times];
                [QRCode setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"qrcodeUrl"]]];
                UIButton* logo=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 30,30) ];
                [logo setImageWithURL:[NSURL URLWithString:[severDataDict.dataDict  objectForKey:@"logoUrl"]]];
                logo.center=QRCode.center;
                [self.view addSubview:logo];

                UILabel* prompt=[[UILabel alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-144,ScreenWidth,12)];
                prompt.text=@"对准二维码，即可自动扫描";
                prompt.textColor=zhonghuiColor;
                prompt.textAlignment = NSTextAlignmentCenter;
                
                prompt.font=[UIFont systemFontOfSize:12];
                [self.view addSubview:prompt];
              
                UIButton* qujingkuang=[[UIButton alloc]initWithFrame:CGRectMake(0,0, 550/2,550/2) ];
                [qujingkuang setBackgroundImage:[UIImage imageNamed:@"qujingkuang"] forState:UIControlStateNormal];
                qujingkuang.center=QRCode.center;
                [self.view addSubview:qujingkuang];
                
                
                UILabel* prompt1=[[UILabel alloc]initWithFrame:CGRectMake(0, qujingkuang.bottom+15,ScreenWidth,12)];
                prompt1.text=@"扫一扫关注公众号，立刻成为会员，享会员优惠！";
                prompt1.textColor=luseColor;
                prompt1.textAlignment = NSTextAlignmentCenter;
                
                prompt1.font=[UIFont systemFontOfSize:12];
                [self.view addSubview:prompt1];
}else{
        [Global hideProgressHUDInternal:self.view];
        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
        [Global showTextHudSub:requestResultStr view:self.view];
    }
     
     }
     else
     {
         [Global hideProgressHUDInternal:self.view];
        
         [Global showTextHudSub:severDataDict.resultStr view:self.view];
     }
     
     }];
    

    
}

- (void)times{
    time = [[UIButton alloc]init];
    time.frame = CGRectMake(ScreenWidth-100,ScreenHeight*35/2200,30,16);
    [time setTitleColor:luseColor forState:UIControlStateNormal];
    [time setTitle:[NSString stringWithFormat:@"%d",10] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.view addSubview:time];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownActions:) userInfo:time repeats:YES];
    
    
}
//自动更新
-(void)countDownActions:(NSTimer*)timer{
    
    time = timer.userInfo;
    
    if (!time.superview) {
        [timer invalidate];
        return;
    }
    int currentTime = [[time titleForState:UIControlStateNormal] intValue];
    [time setTitle:[NSString stringWithFormat:@"%d",--currentTime] forState:UIControlStateNormal];
    time.titleLabel.font = [UIFont systemFontOfSize:16];
    if (currentTime<1) {
         [time setTitle:[NSString stringWithFormat:@"%d",7] forState:UIControlStateNormal];
        [self times];
        [time removeFromSuperview];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:Token forKey:@"tokenId"];
        [params setValue:sn forKey:@"sn"];
        [HttpRequest requestData:@"Company/GetMemberByQrCode" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                NSLog(@"－－－－－－－%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    
                    [Global hideProgressHUDInternal:self.view];
                    
                    [time removeFromSuperview];
                    [timer invalidate];
 
                    
                    if ([self.JumpStatus isEqualToString:@"AddView"]) {
                        
                        if ([[[severDataDict.dataDict objectForKey:@"memberInfo"]objectForKey:@"phone"]length]>1) {
                            
                            OrderExtAddViewController *homeVC1 = [[OrderExtAddViewController alloc] init];
                            UIViewController *target1 = nil;
                            for (UIViewController * controller in self.navigationController.viewControllers) { //遍历
                                if ([controller isKindOfClass:[homeVC1 class]]) { //这里判断是否为你想要跳转的页面
                                    target1 = controller;
                                }
                            }
                            if (target1) {
                                NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
                                NSString* Token = [user objectForKey:@"tokenId"];
                                NSMutableDictionary *param = [NSMutableDictionary new];
                                [param setValue:Token forKey:@"tokenId"];
                                [param setValue:[severDataDict.dataDict objectForKey:@"memberId"] forKey:@"memberId"];
                                
                                [HttpRequest requestData:@"Business/Member/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
                                    if (isSuccess) {
                                        
                                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                                            commdity.dataMemberDict=severDataDict.dataDict;
                                            [[NSNotificationCenter defaultCenter]postNotificationName:@"updateMemberId" object:nil userInfo:severDataDict.dataDict];
                                            [self.navigationController popToViewController:target1 animated:YES]; //跳转
                                        }else{
                                            [Global hideProgressHUDInternal:self.view];
                                        }
                                    }
                                    else
                                    {
                                        [Global hideProgressHUDInternal:self.view];
                                        [Global showTextHudSub:severDataDict.resultStr view:self.view];
                                    }
                                }];
                                }else{
                                
                                MembershipDetailsViewController * Login=[[MembershipDetailsViewController alloc]init];
                                Login.memberId=[severDataDict.dataDict objectForKey:@"memberId"];
                                [self.navigationController pushViewController:Login animated:YES];

                            }
                            
                        }else{
                           
                            AddMemberViewController * Login=[[AddMemberViewController alloc]init];
                            Login.headImgUrl=[severDataDict.dataDict objectForKey:@"headImgUrl"];
                            Login.nickName=[severDataDict.dataDict objectForKey:@"nickName"];
                            Login.openID=[severDataDict.dataDict objectForKey:@"openID"];
                            Login.sex=[NSString stringWithFormat:@"%@",  [severDataDict.dataDict objectForKey:@"sex"]];
                            [self.navigationController pushViewController:Login animated:YES];
                        }
                        }else{
              
                         MemberListViewController* Login=[[MemberListViewController alloc]init];
                        [self.navigationController pushViewController:Login animated:YES];

                        
                    }
                
                }else{
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];

                }
         
         }
         else
         {
             
            [timer invalidate];
             [Global showTextHudSub:severDataDict.resultStr view:self.view];
         }
         
         }];
    }
}
-(void)next{
}
-(void)cancel:(UIButton *)sender{
  [time removeFromSuperview];
 [self.navigationController popViewControllerAnimated:YES];


}
-(void)BackAction:(UIButton *)sender{
    [time removeFromSuperview];
//  commdity.dataMemberDict=nil;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setNavigation{
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.title = @"扫公众号二维码";
//    if ([self.QrCode isEqualToString:@"GetReadMemberQrCode"]) {
//        
//        UIButton*cancel = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, ScreenWidth*65/750, 30)];
//        [cancel setTitle:@"取消" forState:UIControlStateNormal];
//        [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        cancel.titleLabel.font=[UIFont systemFontOfSize:contenfot];
//        [cancel addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
//        UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:cancel];
//        self.navigationItem.rightBarButtonItem = myBtnBtn;
//
//    }else{
//   
//    }
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}



@end
